/**
 * @Author zhang lei
 * @Date 2021-11-14 20:36
 */
public class Algo123股票买卖最多2次 {
    public static void main(String[] args) {
        int[] prices = {3,3,5,0,0,3,1,4};
        int[] prices2 = {1,2,3,4,5};
        System.out.println(maxProfit(prices2));
    }

    public static int maxProfit(int[] prices) {
        int n = prices.length;
        int[][][] dp = new int[n][3][2];

        for (int i = 0; i < n; i++) {
            for (int j = 1; j <= 2; j++) {
                if(i == 0) {
                    dp[i][j][0] = 0;
                    dp[i][j][1] = -prices[i];
                    continue;
                }
                dp[i][j][0] = Math.max(dp[i-1][j][0], dp[i-1][j][1] + prices[i]);
                dp[i][j][1] = Math.max(dp[i-1][j][1], dp[i-1][j-1][0] - prices[i]);
            }
        }
        return dp[n-1][2][0];
    }
}
